package com.nq.service.impl;

/**

 * 开发者----股牛网络

 * 联系TG：gncms

 */


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nq.common.ServerResponse;

import com.nq.dao.SiteSettingMapper;

import com.nq.dao.SiteAdminIndexMapper;
import com.nq.dao.StHolidaysMapper;
import com.nq.pojo.SiteSetting;

import com.nq.pojo.StHolidays;
import com.nq.pojo.StHolidaysPo;
import com.nq.service.ISiteSettingService;

import java.util.Date;
import java.util.List;

import com.nq.utils.DateTimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


@Service("iSiteSettingService")
public class SiteSettingServiceImpl implements ISiteSettingService {

    @Autowired
    SiteSettingMapper siteSettingMapper;
    @Autowired
    SiteAdminIndexMapper siteAdminIndexMapper;
    @Autowired
    private StHolidaysMapper holidaysMapper;
    @Value("${spring.profiles.active}")
    private String active;


    public SiteSetting getSiteSetting() {

        SiteSetting siteSetting = null;

        List list = this.siteSettingMapper.findAllSiteSetting();

        if (list.size() > 0) {

            siteSetting = (SiteSetting) list.get(0);

        }
        return siteSetting;
    }


    public ServerResponse update(SiteSetting setting) {
        if (setting.getId() == null) {
            return ServerResponse.createByErrorMsg("ID can't be empty.\n");
        }
        SiteSetting siteSetting = this.siteSettingMapper.selectByPrimaryKey(setting.getId());
        if (siteSetting == null) {
            return ServerResponse.createByErrorMsg("The setup record cannot be found.");
        }

        int updateCount = this.siteSettingMapper.updateByPrimaryKeySelective(setting);

        if (updateCount > 0) {
            return ServerResponse.createBySuccessMsg("Successful modification");
        }
        return ServerResponse.createByErrorMsg("Modification Failed");

    }

    @Override
    public ServerResponse addHoliday(StHolidaysPo holidays) {
        String holiday = holidays.getHoliday();
        StHolidays stHolidays = new StHolidays();
        stHolidays.setHoliday(DateTimeUtil.strToDate(holiday));
        stHolidays.setName(holidays.getName());

        return holidaysMapper.insert(stHolidays) > 0 ? ServerResponse.createBySuccessMsg("Successful addition") : ServerResponse.createByErrorMsg("Addition failed");
    }

    @Override
    public ServerResponse updateHoliday(StHolidaysPo holidays) {
        String holiday = holidays.getHoliday();
        StHolidays stHolidays = new StHolidays();
        stHolidays.setId(holidays.getId());
        stHolidays.setHoliday(DateTimeUtil.strToDate(holiday));
        stHolidays.setName(holidays.getName());
        return holidaysMapper.updateById(stHolidays) > 0 ? ServerResponse.createBySuccessMsg("Successful modification") : ServerResponse.createByErrorMsg("Modification failed");
    }

    @Override
    public ServerResponse queryHoliday(String name, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        QueryWrapper<StHolidays> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(name), "name", name);

        List<StHolidays> stHolidays = holidaysMapper.selectList(wrapper);
        PageInfo<StHolidays> pageInfo = new PageInfo<>(stHolidays);

        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse delHoliday(Integer id) {
        return ServerResponse.createBySuccess(holidaysMapper.deleteById(id));
    }


}
